package utils;

import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import models.Comment;

public class DAOComment extends DAO{
	
	public DAOComment() throws Exception{
		super();
	}
	
	
	public void addNewComment(String tweetId, String user, String comment) throws SQLException, NoSuchAlgorithmException{
		String query = "INSERT INTO `ts1`.`comment` (`idtweet`,  `user`,  `comment`, datetime) VALUES ('"+tweetId+ "', '" + user+ "', '"+ comment+"', NOW())";
		Statement sentencia = connexio.createStatement();
		sentencia.execute(query);
	}
	
	public void deleteComment(int commentId) throws SQLException, NoSuchAlgorithmException {
		Statement sentencia = connexio.createStatement();
		String query = "DELETE FROM comment WHERE idcomment = '"+ commentId+"'";
		sentencia.execute(query);
	}
	
	public ArrayList<Comment> getTweetComments(int tweetId) throws SQLException, NoSuchAlgorithmException{
		ArrayList<Comment> comments = new ArrayList<Comment>();
		Statement sentencia = connexio.createStatement();
		try{
			String query = "SELECT * FROM `ts1`.`comment` WHERE idtweet ='" + tweetId + "';";
			ResultSet result = sentencia.executeQuery(query);
			while(result.next()) {
				Comment comment = new Comment(Integer.parseInt(result.getString("idcomment")),Integer.parseInt(result.getString("idtweet")), result.getString("user"),  result.getString("comment"), result.getTimestamp("datetime"));
				comments.add(comment);
			}
		}		
		 catch (Exception ex){
			ex.printStackTrace();
		}
		
		return comments;
	}
	
}
